package com.egene.tw.activity.settings;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.egene.tw.R;
import com.egene.tw.activity.login.LoginActivity_;
import com.egene.tw.activity.settings.devicemanage.DeviceManageActivity_;
import com.egene.tw.blue.BluetoothLeServiceV2;
import com.egene.tw.dao.UserDao;
import com.egene.tw.utils.ImageUtil;
import com.viewpagerindicator.CirclePageIndicator;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

@EFragment(R.layout.fragment_settings)
public class SettingsFragment extends Fragment {
    private int[] img = {R.drawable.setting_1, R.drawable.setting_2, R.drawable.setting_3};
    @ViewById
    ViewPager vpImage;
    @ViewById
    CirclePageIndicator cPageIndicator;

    @Bean
    UserDao userDao;

    boolean mCanImgRunning = false;
//    ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();

//    ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(1);

    Thread mThread;

    @AfterViews
    void deal() {
        list = new ArrayList<View>();
        for (int i = 0; i < img.length; i++) {
            ImageView imgView = new ImageView(getActivity());
            imgView.setScaleType(ImageView.ScaleType.FIT_XY);
            ImageUtil.loadImage(getActivity(), imgView, img[i]);
//            Picasso.with(getActivity()).load(img[i]).into(imgView);
            list.add(imgView);
        }
        vpImage.setAdapter(new ViewPagerAdapter(list));
        cPageIndicator.setViewPager(vpImage);
    }


    @Override
    public void onStart() {
        startImgRun();
        super.onStart();
    }

    @Override
    public void onStop() {
        stopImgRun();
        super.onStop();
    }

    /**
     * 开始图片轮播
     */
    private void startImgRun() {
        mCanImgRunning = true;
        mThread = new Thread() {
            @Override
            public void run() {
                while (mCanImgRunning) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (!mCanImgRunning) {
                        return;
                    }
                    if (vpImage == null) {
                        return;
                    }
                    Log.d("seeeee", "runding!!!");
                    if (vpImage.getCurrentItem() == img.length - 1) {
                        vpImage.post(new Runnable() {
                            @Override
                            public void run() {
                                if (vpImage == null) {
                                    return;
                                }
                                vpImage.setCurrentItem(0, true);
                            }
                        });
                    } else {
                        vpImage.post(new Runnable() {
                            @Override
                            public void run() {
                                if (vpImage == null) {
                                    return;
                                }
                                vpImage.setCurrentItem(vpImage.getCurrentItem() + 1, true);
                            }
                        });
                    }
                }
            }
        };
        mThread.start();

    }

    /**
     * 停止图片轮播
     */
    private void stopImgRun() {
        mCanImgRunning = false;
        mThread.interrupt();
    }


    @Click(R.id.llAccount)
    void goToAccount() {
        Intent intent = new Intent(getActivity(), AccountActivity_.class);
        startActivity(intent);

    }

    @Click(R.id.llAboutMe)
    void goToAboutMe() {
        Intent intent = new Intent(getActivity(), AboutMeActivity_.class);
        startActivity(intent);
    }

    @Click(R.id.llDeviceManage)
    void goToDeviceManage() {
        Intent intent = new Intent(getActivity(), DeviceManageActivity_.class);
        startActivity(intent);
    }

    @Click(R.id.ll_change_phone)
    void goToChangePhone() {
        Intent intent = new Intent(getActivity(), ChangePhoneActivity_.class);
        startActivity(intent);
    }

    @Click(R.id.llUpdate)
    void checkUpdate() {
//        UpdateUtils.update(getActivity());
    }

    @Click(R.id.btnLoginOut)
    void loginOut() {
        userDao.removeUser();
        LoginActivity_.intent(getActivity()).start();
        getActivity().finish();

        BluetoothLeServiceV2.stop(getActivity());
    }

    private List<View> list;

    /**
     * 图片滑动适配器
     */
    class ViewPagerAdapter extends PagerAdapter {
        private List<View> viewList;

        public ViewPagerAdapter(List<View> viewList) {
            this.viewList = viewList;
        }

        @Override
        public int getCount() {
            return viewList.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewList.get(position));//删除页卡
        }


    }
}
